const express = require('express');
const router = express.Router();
const WalletUtil = require('../util/walletUtil');
const wallet = new WalletUtil();
const reg = /^(([1-9][0-9]*\.[0-9][0-9]*)|([0]\.[0-9][0-9]*)|([1-9][0-9]*)|([0]{1}))$/;
router.get('/', function (req, res, next) {
    res.render('wallet');
});
/**
 * 创建钱包
 */
router.get('/create', function (req, res) {
    let rs = {code: 0, msg: '', info: []};
    let pwd = req.query.pwd;
    if (!pwd || pwd.trim().length == 0) {
        rs.code = -1;
        rs.msg = '请输入密码';
        res.json(rs);
    } else {
        wallet.createWallet(pwd, function (walletInfo) {
            if (walletInfo.code == 0) {
                rs.info[0] = walletInfo.data;
            }
            else {
                rs.code = -1;
                rs.msg = walletInfo.msg;
            }
            res.json(rs);
        })
    }
});

/**
 * 导入私钥
 */
router.get('/importPriKey', function (req, res) {
    let rs = {code: 0, msg: '', info: []};
    let pri = req.query.pri;
    if (!pri || pri.trim().length == 0) {
        rs.code = -1;
        rs.msg = '请输入私钥';
        res.json(rs);
        return;
    }
    let pwd = req.query.pwd;
    if (!pwd || pwd.trim().length == 0) {
        rs.code = -1;
        rs.msg = '请输入密码';
        res.json(rs);
        return;
    }
    wallet.importWalletByPrivateKey(pri, pwd, function (walletInfo) {
        if (walletInfo.code == 0) {
            rs.info[0] = walletInfo.data;
        }
        else {
            rs.code = -1;
            rs.msg = walletInfo.msg;
        }
        res.json(rs);
    })
});

/**
 * 导入助记词
 */
router.get('/importSeed', function (req, res) {
    let rs = {code: 0, msg: '', info: []};
    let seed = req.query.seed;
    if (!seed || seed.trim().length == 0) {
        rs.code = -1;
        rs.msg = '请输入私钥';
        res.json(rs);
        return;
    }
    let pwd = req.query.pwd;
    if (!pwd || pwd.trim().length == 0) {
        rs.code = -1;
        rs.msg = '请输入密码';
        res.json(rs);
        return;
    }
    wallet.importWalletBySeed(seed, pwd, function (walletInfo) {
        if (walletInfo.code == 0) {
            rs.info[0] = walletInfo.data;
        }
        else {
            rs.code = -1;
            rs.msg = walletInfo.msg;
        }
        res.json(rs);
    })
});

/**
 * 导入keystore
 */
router.get('/importKeystore', function (req, res) {
    let rs = {code: 0, msg: '', info: []};
    let keystore = req.query.keystore;
    if (!keystore || keystore.trim().length == 0) {
        rs.code = -1;
        rs.msg = '请输入keystore';
        res.json(rs);
        return;
    }
    let pwd = req.query.pwd;
    if (!pwd || pwd.trim().length == 0) {
        rs.code = -1;
        rs.msg = '请输入密码';
        res.json(rs);
        return;
    }
    wallet.importWalletByKeyStore(keystore, pwd, function (walletInfo) {
        if (walletInfo.code == 0) {
            rs.info[0] = walletInfo.data;
        }
        else {
            rs.code = -1;
            rs.msg = walletInfo.msg;
        }
        res.json(rs);
    })
});

router.get('/getEthBalance', function (req, res) {
    let rs = {code: 0, msg: '', info: []};
    let addr = req.query.addr;
    if (!addr || addr.trim().length == 0) {
        rs.code = -1;
        rs.msg = '请输入钱包地址';
        res.json(rs);
        return;
    }
    wallet.getEthBalance(addr)
        .then(info => {
            if (info.code == 0) {
                rs.info[0] = info.data;
            }
            else {
                rs.code = -1;
                rs.msg = info.msg;
            }
            res.json(rs);
        }, err => {
            rs.code = -1;
            rs.msg = err.message;
            res.json(rs);
        });
});

router.get('/getTokenBalance', function (req, res) {
    let rs = {code: 0, msg: '', info: []};
    let addr = req.query.addr;
    if (!addr || addr.trim().length == 0) {
        rs.code = -1;
        rs.msg = '请输入钱包地址';
        res.json(rs);
        return;
    }
    let token = req.query.token;
    if (!token || token.trim().length == 0) {
        rs.code = -1;
        rs.msg = '请输入代币地址';
        res.json(rs);
        return;
    }
    wallet.getTokenBalance(addr, token)
        .then(info => {
            if (info.code == 0) {
                rs.info[0] = info.data;
            }
            else {
                rs.code = -1;
                rs.msg = info.msg;
            }
            res.json(rs);
        }, err => {
            rs.code = -1;
            rs.msg = err.message;
            res.json(rs);
        });
});

router.get('/sendEthTransaction', function (req, res) {
    let rs = {code: 0, msg: '', info: []};
    let fromAddr = req.query.fromAddr;
    if (!fromAddr || fromAddr.trim().length == 0) {
        rs.code = -1;
        rs.msg = '请输入出款方地址';
        res.json(rs);
        return;
    }
    let pri = req.query.pri;
    if (!pri || pri.trim().length == 0) {
        rs.code = -1;
        rs.msg = '请输入出款方私钥';
        res.json(rs);
        return;
    }
    let toAddr = req.query.toAddr;
    if (!toAddr || toAddr.trim().length == 0) {
        rs.code = -1;
        rs.msg = '请输入收款方地址';
        res.json(rs);
        return;
    }
    let amount = req.query.amount;
    if (!amount || !reg.test(amount) || !parseFloat(amount) > 0) {
        rs.code = -1;
        rs.msg = '请输入正确的金额';
        res.json(rs);
        return;
    }
    wallet.sendEthTransaction(fromAddr, pri, toAddr, amount)
        .then(info => {
            if (info.code == 0) {
                rs.info[0] = info.data;
            }
            else {
                rs.code = -1;
                rs.msg = info.msg;
            }
            res.json(rs);
        }, err => {
            rs.code = -1;
            rs.msg = err.message;
            res.json(rs);
        });
});

router.get('/sendTokenTransaction', function (req, res) {
    let rs = {code: 0, msg: '', info: []};
    let fromAddr = req.query.fromAddr;
    if (!fromAddr || fromAddr.trim().length == 0) {
        rs.code = -1;
        rs.msg = '请输入出款方地址';
        res.json(rs);
        return;
    }
    let pri = req.query.pri;
    if (!pri || pri.trim().length == 0) {
        rs.code = -1;
        rs.msg = '请输入出款方私钥';
        res.json(rs);
        return;
    }
    let toAddr = req.query.toAddr;
    if (!toAddr || toAddr.trim().length == 0) {
        rs.code = -1;
        rs.msg = '请输入收款方地址';
        res.json(rs);
        return;
    }
    let tokenAddr = req.query.tokenAddr;
    if (!tokenAddr || tokenAddr.trim().length == 0) {
        rs.code = -1;
        rs.msg = '请输入代币合约地址';
        res.json(rs);
        return;
    }
    let amount = req.query.amount;
    if (!amount || !reg.test(amount) || !parseFloat(amount) > 0) {
        rs.code = -1;
        rs.msg = '请输入正确的金额';
        res.json(rs);
        return;
    }
    wallet.sendTokenTransaction(fromAddr, pri, toAddr, tokenAddr, amount)
        .then(info => {
            if (info.code == 0) {
                rs.info[0] = info.data;
            }
            else {
                rs.code = -1;
                rs.msg = info.msg;
            }
            res.json(rs);
        }, err => {
            rs.code = -1;
            rs.msg = err.message;
            res.json(rs);
        });
});
module.exports = router;
